﻿using AdinZ.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;


/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/3/31 16:43:38
*
* */
namespace Novel.Sys.Dal
{
    /* 小说章节 */
    public class novel_chapter
    {
        public int Add(Model.novel_chapter model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO [novel_chapter] ([aid], [chaptername], [chapternum], [size], [created], [modified], [isvip], [price], [status], [articlepath],[CollectionMark]) VALUES (");
            strSql.Append("@aid,@chaptername,@chapternum,@size,@created,@modified,@isvip,@price,@status,@articlepath,@CollectionMark");
            strSql.Append(")select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@aid", SqlDbType.Int),
                    new SqlParameter("@chaptername", SqlDbType.NVarChar,100),
                    new SqlParameter("@chapternum", SqlDbType.Int),
                    new SqlParameter("@size", SqlDbType.Int),
                    new SqlParameter("@created", SqlDbType.DateTime),
                    new SqlParameter("@modified", SqlDbType.DateTime),
                    new SqlParameter("@isvip", SqlDbType.Int),
                    new SqlParameter("@price", SqlDbType.Int),
                    new SqlParameter("@status", SqlDbType.Int),
                    new SqlParameter("@articlepath", SqlDbType.NVarChar,200),
                    new SqlParameter("@CollectionMark",SqlDbType.NVarChar,50)};
            parameters[0].Value = model.Aid;
            parameters[1].Value = model.Chaptername;
            parameters[2].Value = model.Chapternum;
            parameters[3].Value = model.Size;
            parameters[4].Value = model.Created;
            parameters[5].Value = model.Modified;
            parameters[6].Value = model.Isvip;
            parameters[7].Value = model.Price;
            parameters[8].Value = model.Status;
            parameters[9].Value = model.Articlepath;
            parameters[10].Value = model.CollectionMark;
            object obj = DbHelpers.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        //public bool Exist(string str) { }

        public int GetCount(int Aid)
        {
            //select count(1) from [novel_chapter] WHERE aid='10'
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [novel_chapter]");
            strSql.Append(" where aid=@aid");
            SqlParameter[] parameters = {
                    new SqlParameter("@aid", SqlDbType.Int)};
            parameters[0].Value = Aid;

            object obj = DbHelpers.GetSingle(strSql.ToString(), parameters);
            int cmdresult;
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            return cmdresult;


        }

        public bool Exists(int Aid, string chapterName, string CollectionMark)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [novel_chapter]");
            strSql.Append(" where aid=@aid and chaptername=@chaptername and CollectionMark=@CollectionMark");
            SqlParameter[] parameters = {
                    new SqlParameter("@aid", SqlDbType.NVarChar,20),
                    new SqlParameter("@chaptername", SqlDbType.NVarChar,100),
                    new SqlParameter("@CollectionMark", SqlDbType.NVarChar,50)};
            parameters[0].Value = Aid;
            parameters[1].Value = chapterName;
            parameters[2].Value = CollectionMark;
            return DbHelpers.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM novel_chapter ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelpers.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strWhere = " Where " + strWhere;
            }

            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                orderby = " Order By " + orderby;
            }

            //startIndex = startIndex - 1;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM novel_chapter Where ID Not IN ");
            strSql.Append("(Select Top " + startIndex + " ID From novel_chapter" + strWhere + orderby + ")");
            strSql.Append(" And ID In ");
            strSql.Append("(Select Top " + endIndex + " ID From novel_chapter" + strWhere + orderby + ")");
            strSql.Append(orderby);
            return DbHelpers.GetDataSet(strSql.ToString());
        }



        /// <summary>
        /// 获取不是本地的数据
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public DataSet GetRemoteList(int aid)
        {
            string sql = "select  id,articlepath,chaptername from [novel_chapter] where [aid]='" + aid + "' AND local=0";
            return DbHelpers.GetDataSet(sql.ToString());
        }
        public int UpdateChapterPath(int id, string ChapterPath, int local,long size)
        {
            string sql = "UPDATE novel_chapter SET [local] = '" + local + "' ,[size]='" + size + "' , [articlepath]='" + ChapterPath + "' WHERE id='" + id + "'";
            return DbHelpers.NonQuery(sql);
        }

        /// <summary>
        /// 获取最后一章修改时间
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetModified(int id)
        {
            string sql = "select top 1 modified from novel_chapter where [aid]='" + id + "' Order by chapternum desc";
            DataSet ds = DbHelpers.GetDataSet(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DateTime dt = DateTime.Parse(ds.Tables[0].Rows[0]["modified"].ToString());
                return dt.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM novel_chapter ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelpers.GetDataSet(strSql.ToString());
        }


        public Model.novel_chapter GetModel(int ID)
        {
            Model.novel_chapter model = new Model.novel_chapter();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from [novel_chapter] where ID=@ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.Int)};
            parameters[0].Value = ID;
            DataSet ds = DbHelpers.GetDataSet(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["aid"] != null && ds.Tables[0].Rows[0]["aid"].ToString() != "")
                {
                    model.Aid = int.Parse(ds.Tables[0].Rows[0]["aid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["chaptername"] != null && ds.Tables[0].Rows[0]["chaptername"].ToString() != "")
                {
                    model.Chaptername = ds.Tables[0].Rows[0]["chaptername"].ToString();
                }
                if (ds.Tables[0].Rows[0]["chapternum"] != null && ds.Tables[0].Rows[0]["chapternum"].ToString() != "")
                {
                    model.Chapternum = int.Parse(ds.Tables[0].Rows[0]["chapternum"].ToString());
                }
                if (ds.Tables[0].Rows[0]["size"] != null && ds.Tables[0].Rows[0]["size"].ToString() != "")
                {
                    model.Size = int.Parse(ds.Tables[0].Rows[0]["size"].ToString());
                }

                if (ds.Tables[0].Rows[0]["created"] != null && ds.Tables[0].Rows[0]["created"].ToString() != "")
                {
                    model.Created = DateTime.Parse(ds.Tables[0].Rows[0]["created"].ToString());
                }

                if (ds.Tables[0].Rows[0]["modified"] != null && ds.Tables[0].Rows[0]["modified"].ToString() != "")
                {
                    model.Modified = DateTime.Parse(ds.Tables[0].Rows[0]["modified"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isvip"] != null && ds.Tables[0].Rows[0]["isvip"].ToString() != "")
                {
                    model.Isvip = int.Parse(ds.Tables[0].Rows[0]["isvip"].ToString());
                }
                if (ds.Tables[0].Rows[0]["price"] != null && ds.Tables[0].Rows[0]["price"].ToString() != "")
                {
                    model.Price = double.Parse(ds.Tables[0].Rows[0]["price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["status"] != null && ds.Tables[0].Rows[0]["status"].ToString() != "")
                {
                    model.Status = int.Parse(ds.Tables[0].Rows[0]["status"].ToString());
                }
                if (ds.Tables[0].Rows[0]["local"] != null && ds.Tables[0].Rows[0]["local"].ToString() != "")
                {
                    model.Local = int.Parse(ds.Tables[0].Rows[0]["local"].ToString());
                }
                if (ds.Tables[0].Rows[0]["articlepath"] != null && ds.Tables[0].Rows[0]["articlepath"].ToString() != "")
                {
                    model.Articlepath = ds.Tables[0].Rows[0]["articlepath"].ToString();
                }
                return model;
            }
            else
                return null;

        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM novel_chapter ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelpers.GetDataSet(strSql.ToString());
        }
    }

}

